package com.ybkj.o2o.sales.manager.impl;

import com.ybkj.o2o.sales.common.DataGrid;
import com.ybkj.o2o.sales.entity.CouponRule;
import com.ybkj.o2o.sales.entity.CouponRule.CouponType;
import com.ybkj.o2o.sales.manager.CouponRuleManager;
import com.ybkj.o2o.sales.service.CouponRuleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class DefaultCouponRuleManager implements CouponRuleManager {

  @Autowired
  private CouponRuleService couponRuleService;


  /**
   * 事务回滚.
   */
  @Transactional(rollbackFor = Exception.class)
  public CouponRule insert(String title, String code, Date start, Date end, Long companyId,
      Integer type, String couponType, Long couponTypeId, Double discount, BigDecimal money, Integer number,
      Integer eachLimit, String appKey) {
    CouponRule couponRule = new CouponRule();
    couponRule.setTitle(title);
    couponRule.setType(type);
    couponRule.setCouponType(CouponType.valueOf(couponType));
    couponRule.setCouponTypeId(couponTypeId);
    if (null == couponRuleService.findByCode(code, appKey)) {
      couponRule.setCode(code);
    } else {
      throw new RuntimeException("该编码已存在!!");
    }
    couponRule.setStart(start);
    couponRule.setEnd(end);
    couponRule.setCompanyId(companyId);
    if (type == 0) {
      couponRule.setDiscount(discount);
    } else {
      couponRule.setMoney(money);
    }
    couponRule.setNumber(number);
    couponRule.setEachLimit(eachLimit);
    couponRule.setAppKey(appKey);
    Long integer = couponRuleService.insert(couponRule);
    if (integer != 1) {
      throw new RuntimeException("创建失败!!");
    }
    return couponRule;
  }

  /**
   * 事务回滚.
   */
  @Transactional(rollbackFor = Exception.class)
  public void delete(Long id, String appKey) {

    couponRuleService.delete(id, appKey);

  }

  /**
   * 事务回滚.
   */
  @Transactional(rollbackFor = Exception.class)
  public int update(Long id, String appKey) {
    if (id != null) {
      CouponRule couponRule = couponRuleService.findOne(id, appKey);
      if (null != couponRule) {
        couponRule.setLapsed(true);
      }
      return couponRuleService.update(couponRule);
    } else {
      throw new RuntimeException("没有可以操作的数据");
    }


  }

  @Override
  public DataGrid<CouponRule> queryAll(Long companyId, Integer page, Integer rows, String appKey) {

    Integer offset = 0;
    if (page != null && rows != null) {
      offset = (page - 1) * rows;
    }
    List<CouponRule> list = couponRuleService.selectByCompany(companyId, offset, rows, appKey);
    long count = couponRuleService.countByCompany(companyId, appKey);

    DataGrid<CouponRule> dataGrid = new DataGrid<CouponRule>();
    dataGrid.setRows(list);
    dataGrid.setTotal(count);

    return dataGrid;
  }

  @Override
  public List<CouponRule> queryValid(Long companyId, String appKey) {

    return couponRuleService.selectValid(companyId, appKey);
  }

  @Override
  public CouponRule findByIdAndAppKey(Long id, String appKey) {

    return couponRuleService.findOne(id, appKey);

  }



}
